package cn.cs.controller;

import cn.cs.client.RestRepairUserClient;
import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.service.ShopOrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoppingOrder")
public class ShoppingOrderController {
    @Resource
    private ShopOrderService service;

    @Resource
    private RestRepairUserClient repairUserClient;

    /****
     * 生成订单
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/submitOrder")
    @ResponseBody
    public Dto submitOrder(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.submitOrder(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("添加失败","002");
        }
        return dto;
    }


    /****
     * 根据ID查询订单详情接口
     * @param orderId 订单id
     * @param
     * @return
     */
    @RequestMapping("/getOrdInfoById")
    @ResponseBody
    public Dto getOrdInfoById(@RequestParam String orderId, HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            String token = request.getHeader("token");
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.getOrdInfoById(orderId,request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }

    /****
     * 根据ID查询订单详情接口
     * @param
     * @param
     * @return
     */
    @RequestMapping("/plantGetOrdInfoById")
    @ResponseBody
    public Dto plantGetOrdInfoById(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            String token = request.getHeader("token");
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.plantGetOrdInfoById(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }


    /****
     * 查询订单详情接口
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getOrdInfoByUserId")
    @ResponseBody
    public Dto getOrdInfoByUserId( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.getOrdInfoByUserId(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }

    /****
     * 评价购物接口
     * @param
     * @param
     * @return
     */
    @RequestMapping("/evaluate")
    @ResponseBody
    public Dto evaluate( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            String token = request.getHeader("token");
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.evaluate(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }



    /****
     * 查看评价
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getEvaluateByOrderId")
    @ResponseBody
    public Dto getEvaluateByOrderId( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            String token = request.getHeader("token");
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.getEvaluateByOrderId(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }



    /****
     * 购物已收货接口
     * @param
     * @param
     * @return
     */
    @RequestMapping("/hasGetPro")
    @ResponseBody
    public Dto hasGetPro( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            String token = request.getHeader("token");
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.hasGetPro(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }


    /****
     * 取消订单接口
     * @param
     * @param
     * @return
     */
    @RequestMapping("/cancelOrder")
    @ResponseBody
    public Dto cancelOrder( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            String token = request.getHeader("token");
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.cancelOrder(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }

    /**
     * 分页获取订单基础信息 -- 维修工购买订单
     * @param pageNumber
     * @param pageSize
     * @param nickname
     * @param phone
     * @param startDate
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getShopOrderByPage")
    public Dto getShopOrderByPage(Integer pageNumber, Integer pageSize, String nickname, String phone, String startDate, String endDate, String state, String rep_user_id) throws Exception {
        Map<String, Object> param = new HashMap<String,Object>();
        param.put("nickname", nickname);
        param.put("phone", phone);
        param.put("startDate", startDate);
        param.put("endDate", endDate);
        param.put("state", state);
        param.put("rep_user_id", rep_user_id);
        if(EmptyUtils.isNotEmpty(nickname) || EmptyUtils.isNotEmpty(phone)){
            List<Map<String, Object>> list = repairUserClient.getRepairUserByLikePhone(phone, nickname);
            if(EmptyUtils.isEmpty(list) || list.size() <= 0){
                Map<String, Object> m = new HashMap<String, Object>();
                m.put("id", "000");
                list.add(m);
            }
            param.put("list", list);
        }
        return DtoUtil.returnSuccess("获取成功", service.queryShopOrderPageByMap(param, pageNumber, pageSize));
    }


    /**
     * 分页获取订单基础信息 -- 经销商订单
     * @param pageNumber
     * @param pageSize
     * @param nickname
     * @param phone
     * @param startDate
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getShopOrderAgentByPage")
    public Dto getShopOrderAgentByPage(Integer pageNumber, Integer pageSize, String nickname, String phone, String startDate, String endDate, String state) throws Exception {
        Map<String, Object> param = new HashMap<String,Object>();
        param.put("nickname", nickname);
        param.put("phone", phone);
        param.put("startDate", startDate);
        param.put("endDate", endDate);
        param.put("state", state);

        if(EmptyUtils.isNotEmpty(nickname) || EmptyUtils.isNotEmpty(phone)){
            List<Map<String, Object>> list = repairUserClient.getRepairUserByLikePhone(phone, nickname);
            if(EmptyUtils.isEmpty(list) || list.size() <= 0){
                Map<String, Object> m = new HashMap<String, Object>();
                m.put("id", "000");
                list.add(m);
            }
            param.put("list", list);
        }
        return DtoUtil.returnSuccess("获取成功", service.queryShopOrderPageByMap(param, pageNumber, pageSize));
    }

    /**
     * 根据经销商id 查询  商城订单
     * @param pageNumber
     * @param pageSize
     * @param agentId
     * @param startDate
     * @param endDate
     * @param state
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getShopOrderAgentPageByMap")
    public Dto getShopOrderAgentPageByMap(Integer pageNumber, Integer pageSize, String agentId, String startDate, String endDate, String state) throws Exception {
        Map<String, Object> param = new HashMap<String,Object>();
        param.put("agentId", agentId);
        param.put("startDate", startDate);
        param.put("endDate", endDate);
        param.put("state", state);
        return DtoUtil.returnSuccess("获取成功", service.queryShopOrderAgentPageByMap(param, pageNumber, pageSize));
    }



    /**
     * 查询订单产品详情
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getShopOrderDetails")
    public Dto getShopOrderDetails(String id) throws Exception {
        return DtoUtil.returnSuccess("获取成功", service.getShopOrderDetails(id));
    }
}
